package com.lenovo.dh.voteclient;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.lenovo.dh.voteclient.bean.Topics;
import com.lenovo.nova.util.debug.slog;
import com.lenovo.nova.util.network.NetworkTask;
import com.lenovo.nova.util.network.NetworkTask.ResponseNotify;
import com.lenovo.nova.util.parse.Bean;
import com.lenovo.nova.util.parse.JsonToBeanParser;
import com.lenovo.nova.util.parse.JsonToBeanParser.OnJSONFillBeanHelper;
import com.lenovo.nova.util.parse.anntation.json.JsonFieldName;

/**
 * A placeholder fragment containing a simple view.
 */
public class PlaceholderFragment extends Fragment implements OnItemClickListener {

	ListView mListView;
	
	MyAdapter mAdapter = null;
	
	NetworkTask mNetworkTask;
	
	Topics topics = new Topics();
	private List<Topics.Topic> mAllTopic = topics.mList;

	private String rootUrl = "http://10.110.58.168:81/myserver";
	
	public PlaceholderFragment() {
		
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mNetworkTask = new NetworkTask(getActivity());
	}
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View rootView = inflater.inflate(R.layout.fragment_main, container, false);
		mListView = (ListView) rootView.findViewById(R.id.id_lstview_topic);
		mAdapter = new MyAdapter();
		mListView.setAdapter(mAdapter);
		mListView.setOnItemClickListener(this);
		return rootView;
	}
	
	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		
		mNetworkTask.executeRequest(false, getTopicURL(), null, null, null, new ResponseNotify() {
			@Override
			public void onGetResult(String content) {
				slog.p("content is " + content);
				if(content != null){
					JSONArray jsonArray = null;
					try {
						jsonArray = new JSONArray(content);
					} catch (JSONException e) {
						e.printStackTrace();
					}
					JsonToBeanParser.getInstance().fillBeanWithJson(mAllTopic, jsonArray, new OnJSONFillBeanHelper() {
						@Override
						public Bean getNewBean() {
							return topics.newEntity();
						}
					});
					mAdapter.notifyDataSetChanged();
				}else{
					//debug
					mAllTopic.add(topics.newEntity().setTitle("联想电视明星"));
					mAllTopic.add(topics.newEntity().setTitle("联想奖金方案"));
					mAdapter.notifyDataSetChanged();
				}
			}
		});
		
		super.onActivityCreated(savedInstanceState);
	}

	private String getTopicURL() {
		return  rootUrl   +"/vote?method=gettopic";
	}
	
	
	
	
	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
		Topics.Topic entity = mAllTopic.get(position);
		Intent intent = new Intent(getActivity(),VoteActivity.class);
		intent.putExtra("VoteActivity.data", entity);
		startActivity(intent);
	}




	class MyAdapter extends BaseAdapter{

		@Override
		public int getCount() {
			return mAllTopic.size();
		}

		@Override
		public Object getItem(int position) {
			return mAllTopic.get(position);
		}

		@Override
		public long getItemId(int position) {
			return 0;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			if(convertView == null){
				convertView = LayoutInflater.from(getActivity()).inflate(android.R.layout.simple_list_item_1, null);
			}
			
			if(convertView instanceof TextView){
				TextView temp = (TextView) convertView;
				temp.setText("" + mAllTopic.get(position).getTitle());
			}
			
			return convertView;
		}
	}
	
	
	
	
}